﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyLibellus
{
    public partial class MantenimentBook : Form
    {
        DataTable dtBooks;
        GestioBook gb;        
        SqlDataAdapter da;
        public MantenimentBook()
        {
            InitializeComponent();
            dtBooks = DBAccess.getDataTAble("book");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            gridBooks.DataSource = dtBooks;
            SqlConnection con = DBAccess.getConnection();
            con.Open();
            da = DBAccess.getAdapter("select * from book", con, dtBooks);
            SqlCommandBuilder builder = new SqlCommandBuilder (da);
            gb = new GestioBook();            

        }

        

        private void btnAfegirBook_Click(object sender, EventArgs e)
        {
            gb.B = null;
            gb.ShowDialog();
            if (gb.Aceptar)
            {

                //Modificar la fila.
                DataRow dr = dtBooks.NewRow();
                dr["isbn"] = gb.B.Isbn;
                dr["short_title"] = gb.B.Short_title;
                dr["long_title"] = gb.B.Long_title;
                dr["publisher_id"] = gb.B.Publisher_id;
                dr["sumary"] = gb.B.Summary;
                dr["awards"] = gb.B.Awards;
                dr["price_paid"] = gb.B.Price_paid.ToString();
                dtBooks.Rows.Add(dr);
                da.Update(dtBooks);
                gb.relacions();
                gridBooks.Refresh();
                
            }


        }

        private void btnModificarBook_Click(object sender, EventArgs e)
        {
            //si la grid esta buida o no s'ha seleccionat cap fila no fa res.
            if (gridBooks.SelectedRows.Count != 0 && gridBooks.SelectedRows.Count!=0)
            {
                Book b = new Book();
                b.Isbn = gridBooks.SelectedRows[0].Cells[0].Value.ToString();
                b.Short_title = gridBooks.SelectedRows[0].Cells[1].Value.ToString();
                b.Long_title = gridBooks.SelectedRows[0].Cells[2].Value.ToString();
                b.Publisher_id = gridBooks.SelectedRows[0].Cells[3].Value.ToString();
                b.Summary = gridBooks.SelectedRows[0].Cells[4].Value.ToString();
                b.Awards = gridBooks.SelectedRows[0].Cells[5].Value.ToString();
                b.Price_paid = Convert.ToDouble(gridBooks.SelectedRows[0].Cells[6].Value.ToString());
                b.LA = BookRelacions.getAuthors(b);
                b.LS = BookRelacions.getSubjects(b);                
                gb.B = b;
                gb.ShowDialog();
                if (gb.Aceptar)
                {

                    //Modificar la fila.
                    DataRow dr = dtBooks.Rows[gridBooks.SelectedRows[0].Index];
                    dr["isbn"] = b.Isbn;
                    dr["short_title"] = b.Short_title;
                    dr["long_title"] = b.Long_title;
                    dr["publisher_id"] = b.Publisher_id;
                    dr["sumary"] = b.Summary;
                    dr["awards"] = b.Awards;
                    dr["price_paid"] = b.Price_paid.ToString();
                    dtBooks.AcceptChanges();

                    da.Update(dtBooks);
                    

                    gb.relacions();

                    
                    gridBooks.Refresh();
                    
                }
                
            }
        }

        

        private void btnSortirAplicacio_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnEliminarBook_Click(object sender, EventArgs e)
        {
            if (gridBooks.SelectedRows.Count != 0)
            {
                if (MessageBox.Show("ESTA SEGUR D'ESBORRAR AQUEST LLIBRE?", "Confirmar Esborrar", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Book b = new Book();
                    b.Isbn = gridBooks.SelectedRows[0].Cells[0].Value.ToString();
                    b.LA = BookRelacions.getAuthors(b);
                    b.LS = BookRelacions.getSubjects(b);
                    //Eliminar els temes i els autors del llibre.
                    BookRelacions.elminarListBook(b);
                    int pos = gridBooks.SelectedRows[0].Index;//posicio del llibre dins la grid.
                    dtBooks.Rows[pos].Delete();
                    da.Update(dtBooks);
                    gridBooks.Refresh();
                }
            }
        }

        
    }
}
